﻿using System;
using System.Drawing;
using OpenTK.Graphics.OpenGL;

namespace MosCraft.Graphics
{
    public class FontPrinter
    {
        public int Colums { get; set; }
        public int Rows { get; set; }
        public int Spacing { get; set; }
        public int RowSpacing { get; set; }

        public struct TextAlign
        {
            public HorAlign HorAlign { get; private set; }
            public VerAlign VerAlign { get; private set; }

            public TextAlign(HorAlign horAlign, VerAlign verAlign)
                : this()
            {
                if ((int)horAlign < -1 || (int)horAlign > 1)
                    throw new ArgumentOutOfRangeException("horAlign");
                if ((int)verAlign < -1 || (int)verAlign > 1)
                    throw new ArgumentOutOfRangeException("verAlign");
                HorAlign = horAlign;
                VerAlign = verAlign;
            }
        }
        public enum HorAlign : sbyte
        {
            Left = -1,
            Center = 0,
            Right = 1
        }
        public enum VerAlign : sbyte
        {
            Top = -1,
            Middle = 0,
            Bottom = 1
        }

        public FontPrinter()
        {
            Colums = 16;
            Rows = 8;
            Spacing = 8;
            RowSpacing = 14;
        }

        public int GetWidth(string value)
        {
            int width = 0;
            foreach (string line in value.Split('\n'))
                width = Math.Max(width, GetLineWidth(value));
            return width;
        }
        int GetLineWidth(string value)
        {
            return value.Length * Spacing;
        }
        public int GetHeight(string value)
        {
            if (value == null)
                throw new ArgumentNullException("value");
            if (value == string.Empty) return 0;
            return (value.Split('\n').Length * RowSpacing);
        }

        public void Write(int x, int y, string value)
        {
            Write(x, y, value, new TextAlign(HorAlign.Left, VerAlign.Top));
        }
        public void Write(int x, int y, string value, TextAlign align)
        {
            if (value == null)
                throw new ArgumentNullException("value");
            if (value == string.Empty) return;
            int w = 256 / Colums;
            int h = 128 / Rows;
            //Display.Texture = Texture;
            GL.Begin(BeginMode.Quads);
            int yy = y - GetHeight(value) * ((int)align.VerAlign + 1) / 2;
            foreach (string line in value.Split('\n'))
            {
                int xx = x - GetWidth(line) * ((int)align.HorAlign + 1) / 2;
                foreach (char c in line)
                {
                    int i = (int)c;
                    if (i > Colums * Rows) i = 0;
                    RectangleF tex = new RectangleF( (i % Colums) / (float)Colums,(i / Colums) / (float)Rows, 1.0f / Colums, 1.0f / Rows);
                    GL.TexCoord2(tex.Left, tex.Top); GL.Vertex2(xx, yy);
                    GL.TexCoord2(tex.Left, tex.Bottom); GL.Vertex2(xx, yy + h);
                    GL.TexCoord2(tex.Right, tex.Bottom); GL.Vertex2(xx + w, yy + h);
                    GL.TexCoord2(tex.Right, tex.Top); GL.Vertex2(xx + w, yy);
                    xx += Spacing;
                }
                yy += RowSpacing;
            }
            GL.End();
        }

    }
}
